package com.langchain4j.simple.config;

import com.langchain4j.simple.service.ChatAssistant;
import com.langchain4j.simple.service.ChatStreamingAssistant;
import com.langchain4j.simple.service.FluxStreamingAssistant;
import dev.langchain4j.model.chat.ChatModel;
import dev.langchain4j.model.chat.StreamingChatModel;
import dev.langchain4j.model.openai.OpenAiChatModel;
import dev.langchain4j.model.openai.OpenAiStreamingChatModel;
import dev.langchain4j.service.AiServices;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.time.Duration;
import java.util.List;

/**
 * @author liyang
 * @version 1.0.0
 * @ClassName LangChainConfig
 * @Description 模型配置
 * @createTime 11:20:00 2025/04/17
 */
@Slf4j
@Configuration(proxyBeanMethods = false)
public class LangChainConfig {

    @Bean("chatLanguageModel")
    public ChatModel chatLanguageModel() {
        log.info("apiKey: {}", System.getenv("DASHSCOPE_KEY"));
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwen-turbo")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)//开启请求日志
                .logResponses(true)//开启响应日志
                .listeners(List.of(new LangChainListener()))
                .maxRetries(5) //设置重试策略
                .timeout(Duration.ofSeconds(10)) //设置超时时间
                .build();
    }

    @Bean
    public ChatAssistant chatAssistant(@Qualifier("chatLanguageModel") ChatModel chatLanguageModel) {
        return AiServices.create(ChatAssistant.class, chatLanguageModel);
    }

    @Bean
    public StreamingChatModel streamingChatLanguageModel() {
        return OpenAiStreamingChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwq-plus")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .logRequests(true)//开启请求日志
                .logResponses(true)//开启响应日志
                .listeners(List.of(new LangChainListener()))
                .timeout(Duration.ofSeconds(10)) //设置超时时间
                .build();
    }

    @Bean
    public ChatStreamingAssistant chatStreamingAssistant(StreamingChatModel streamingChatLanguageModel) {
        return AiServices.create(ChatStreamingAssistant.class, streamingChatLanguageModel);
    }

    @Bean
    public FluxStreamingAssistant fluxStreamingAssistant(StreamingChatModel streamingChatLanguageModel) {
        return AiServices.create(FluxStreamingAssistant.class, streamingChatLanguageModel);
    }

    @Bean("imageRecognitionChatLanguageModel")
    public ChatModel imageRecognitionChatLanguageModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwen-vl-max")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

    @Bean("chatMemoryChatLanguageModel")
    public ChatModel chatMemoryChatLanguageModel() {
        return OpenAiChatModel.builder()
                .apiKey(System.getenv("DASHSCOPE_KEY"))
                .modelName("qwen-long")
                .baseUrl("https://dashscope.aliyuncs.com/compatible-mode/v1")
                .build();
    }

//    @Bean
//    public ChatAssistant aiAssistant(@Qualifier("chatMemoryChatLanguageModel") ChatLanguageModel chatLanguageModel) {
//        return AiServices.builder(ChatAssistant.class)
//                .chatLanguageModel(chatLanguageModel)
//                .chatMemoryProvider(memoryId -> MessageWindowChatMemory.withMaxMessages(10))
//                .build();
//    }
}
